A portion of the disclosure of this patent document contains material which is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure, as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyright rights whatsoever. The following notice applies to the software and data as described below and in the drawing hereto: Copyright(copyright) 1998, Microsoft Corporation, All Rights Reserved.
This invention relates generally to computer languages, and more particularly to method and apparatus for programming in object-based or object-oriented languages.
High-level programming languages enable developers to decompose code into smaller chunks. In non-object-oriented languages the smallest unit of decomposition is a procedure or function. In object-oriented languages, the smallest unit of decomposition is typically a method on a class.
Many modern programming languages also have mechanisms that enable developers to treat these decomposition units as values that can be used as values (passed around, stored in data structures, etc.) and also be used to xe2x80x9ccallxe2x80x9d or xe2x80x9cinvokexe2x80x9d the code chunk that the value represents. Different languages provide different mechanisms.
In non-object-oriented programming languages, the best known of these constructs is the function pointer found in C and C++. In functional programming languages (LISP, ML, Scheme, etc.) this style of programming is common. In object-oriented programming languages, no adequate mechanism for this purpose has been developed.
In order for a programming system (language and runtime) to enable developers to create and use method references, the present invention addresses the following design objectives:
a) defining an entity that defines the shape of the method to be referenced;
b) obtaining a reference to a method;
c) storing this reference in arbitrary data structures;
d) passing the reference to code written by other parties;
e) enabling invocation of the method by anyone holding the method reference; and
f) enabling the holder of a method reference to determine the target object and method being referenced.
Furthermore, the programming system of the present invention accomplishes the above objectives in a strongly typed manner, making it possible for:
a) developers to learn of type mismatch problems early (at compile-time) rather than late (at run-time); and
b) development tools to present information to developers about the shape of components that use delegates.
Prior Solutions to Related Problems
A number of products employ C and C++ like function pointers, such as Microsoft(copyright) Visual C++, Visual Basic and Visual Basic A, all available from Microsoft Corporation. The Windows(copyright) API makes extensive use of function pointers as well. Function pointers of this kind are deficient for a number of reasons:
a) they are not type-safe and result in casting;
b) they are not security-awarexe2x80x94a function pointer is just a pointer that can be inadvertently altered and then invoked, resulting in a system crash; and
c) they don""t allow for accessing the target object and method.
The Microsoft(copyright) Java VM and related products have a xe2x80x9cJava Core Reflectionxe2x80x9d system that provides a primitive form of method reference. A developer can obtain a java.lang.reflect.Method instance. This instance can be used to invoke methods in a late-bound manner. This form of reference is inherently late-bound and therefore inefficient.
According to one example embodiment of the invention, there is provided a method for programming a computer in object-based computer code, comprising using a computer to perform the steps of invoking a first method by calling a second method of an instance of a class wherein the parameters supplied to the second method are supplied to the first method, and the parameter list of the second method matches the parameter list of the first method, and further wherein the first method is identified by name and by matching the parameter list and result type of the first method with the parameter list and result type declared for the class.
According to another feature, the instance of the class is represented by a value that can be assigned to variables and passed from one procedure to another, and program code is used to invoke the second method on a variable that has been assigned a value representing an instance of the class.
In another embodiment, security permissions are associated with each class in an object-based program, and these permissions are used to determine if a call by the first method to the second method is permitted.
In still another embodiment, the first method is created automatically upon declaration of the first class.
In yet another embodiment, the first method is used to handle events of the object-based program.
In still another embodiment, one or more additional first methods are invoked with the call to the second method, wherein one or more corresponding additional sets of parameters are supplied to the second method to be supplied to the one more additional first methods, respectively. Furthermore, an invocation list is associated with the second method, wherein the invocation list specifies the first methods to be invoked. In addition, first methods may be added to and deleted from the invocation list.
In yet other embodiments, the method outlined above is incorporated into a computer program, a compiler, an interpreter and in a programming system including hardware and software.